<!-- 「欢乐赛积分明细」页面 -->
<template>
    <div class="page">
        <uni-nav-bar fixed statusBar :border="false">
            <div class="navigation-title">编辑资料</div>
        </uni-nav-bar>
        <div class="container">
            <div class="edit-wrapper">
                <div class="edit">
                    <div class="avatar">
                        <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="chooseAvatar"></button>
                        <div class="main">
                            <div class="text">我的头像
                            </div>
                        </div>
                        <div class="right">
                            <image class="img" :src="userInfo.avatar || '/src/static/images/common/default-avatar.png'"
                                mode="scaleToFill"></image>
                            <image class="goto"
                                src="https://ustatic.hudongmiao.com/miao/activity/202505/rightArrow3.png"
                                mode="aspectFill"></image>
                        </div>
                    </div>
                    <div class="nickname" @click="onNicknameEdit">
                        <div class="main">
                            <div class="text">我的昵称
                            </div>
                        </div>
                        <div class="right">
                            <div class="text">{{ temNickname }}</div>
                            <image class="goto"
                                src="https://ustatic.hudongmiao.com/miao/activity/202505/rightArrow3.png"
                                mode="aspectFill"></image>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="confirm-button" @click="completeClick">
            <text class="text">确定</text>
        </div>
    </div>
    <ToLoginMask v-if="!isLogin" />
    <CustomTabBar />
    <EditAvatarPopup ref="editAvatarPopup" v-model="temNickname" />
</template>

<script setup lang="ts">
import EditAvatarPopup from "@/components/activity-202505/editAvatarPopup.vue";
import CustomTabBar from "@/components/CustomTabBar.vue";
import ToLoginMask from "@/components/ToLoginMask.vue";
import { useActivity202505 } from "@/stores/activity-202505-store";
import { useUser } from "@/stores/user";
import { navigateTo } from '@/utils/navigate';
import { showToast } from '@/utils/tools';
const activity202505Store = useActivity202505();
const { personInfo202310 } =
  storeToRefs(activity202505Store);

const editAvatarPopup = ref<InstanceType<typeof EditAvatarPopup>>();
const user = useUser();
const { userInfo, isLogin } = storeToRefs(user);
const temAvatarPath = ref('');
const temNickname = ref(userInfo.value.nickname);
const { uploadAvatarAndName } = user;

onLoad(async ()=>{
})

const chooseAvatar = (e: any) => {
  temAvatarPath.value = e.detail.avatarUrl;
};

const onNicknameEdit = ()=>{
    editAvatarPopup.value?.open();
}

const completeClick = async () => {
  if (!temAvatarPath.value && !userInfo.value.avatar) {
    showToast('请上传头像');
    return;
  }

  if (!temNickname) {
    showToast('请填写昵称');
    return;
  }

  userInfo.value.nickname = temNickname.value;


  const activity202310DataUpdate = async () => {
    const activity202310Store = useActivity202505();
    activity202310Store.setPersonInfo202310();
    // activity202310Store.setTeamInviteCompetitionData();
  };

  try {
    // 更新头像和昵称
    await uploadAvatarAndName(temAvatarPath.value);
    navigateTo('my-2025');
    showToast('编辑资料成功');
    activity202310DataUpdate();
  } catch (e) {
    console.error('编辑资料失败', e);
    showToast('编辑资料失败');
  }
};
</script>

<style scoped lang="scss">
.page {

    position: relative;

    width: 750rpx;
    min-height: 95vh;
    overflow-x: hidden;
    overflow-y: scroll;
    background-color: #F4F8FE;

    @media (min-aspect-ratio: 330/568) {
        min-height: 105vh;
    }

    // 隐藏滚动条
    &::-webkit-scrollbar {
        display: none;
    }



    .navigation-title {
        width: 100%;
        font-size: 34rpx;
        display: flex;
        justify-content: center;
        margin-bottom: 12rpx;
        align-items: center;
        font-weight: bold;
    }



    .container {
        position: relative;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;


        .edit-wrapper {
            margin-top: 20rpx;
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;

            .edit {
                width: 728rpx;
                height: 238rpx;
                display: flex;
                flex-direction: column;
                justify-content: flex-start;
                align-items: center;
                background-color: #fff;
                border-radius: 22rpx 22rpx 22rpx 22rpx;

                .avatar {
                    width: 670rpx;
                    height: 119rpx;
                    display: flex;
                    position: relative;
                    justify-content: space-between;
                    align-items: center;

                    .avatar-button {
                        opacity: 0;
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 100%;
                        height: 100%;
                    }

                    .main {
                        display: flex;
                        align-items: center;
                        justify-content: flex-start;
                        margin-left: 40rpx;

                        .text {
                            height: 37rpx;
                            font-weight: 400;
                            font-size: 34rpx;
                            color: #000000;
                            line-height: 37rpx;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                        }
                    }

                    .right {
                        display: flex;
                        align-items: center;
                        justify-content: flex-end;
                        // margin-right: 47rpx;

                        .img {
                            margin-right: 30rpx;
                            height: 82rpx;
                            width: 82rpx;
                            border-radius: 50%;
                            overflow: hidden;
                        }

                        .goto {
                            width: 13rpx;
                            height: 22rpx;
                        }
                    }

                }

                .nickname {
                    width: 670rpx;
                    height: 119rpx;
                    border-top: 2px solid #D8D8D8;
                    display: flex;
                    position: relative;
                    justify-content: space-between;
                    align-items: center;

                    .main {
                        display: flex;
                        align-items: center;
                        justify-content: flex-start;
                        margin-left: 40rpx;

                        .text {
                            height: 37rpx;
                            font-weight: 400;
                            font-size: 34rpx;
                            color: #000000;
                            line-height: 37rpx;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                        }
                    }

                    .right {
                        display: flex;
                        align-items: center;
                        justify-content: flex-end;
                        // margin-right: 47rpx;

                        .text {
                            margin-right: 30rpx;
                            font-weight: 400;
                            font-size: 30rpx;
                            color: #666666;
                            line-height: 37rpx;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                        }

                        .goto {
                            width: 13rpx;
                            height: 22rpx;
                        }
                    }

                }
            }
        }
    }

    .confirm-button {
        position: absolute;
        bottom: calc($tabBarHeight + 30rpx);
        left:100rpx;
        width: 550rpx;
        height: 95rpx;
        border: none;
        box-shadow: none;
        background-image: url('https://ustatic.hudongmiao.com/miao/activity/202505/yellowButton.png');
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        justify-content: center;

        .text {
            width: 371rpx;
            font-size: 32rpx;
            color: #FFFFFF;
            line-height: 49rpx;
            text-align: center;
            padding-bottom: 10rpx;
            text-transform: none;
        }
    }
}
</style>
